package cn.piesat.scanning.business.xz.service.clientimpl;

import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import cn.piesat.scanning.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2024/3/26 10:55
 */
@Service
public class BuildHB_FY4A_AGRI_L3_4000M_QPEServiceImpl implements BuildAlgorithmTaskService {


    @Autowired
    private HtScanningMetadataService htScanningMetadataService;

    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //FY4A-_AGRI--_N_REGC_1047E_L2-_QPE-_MULT_GLL_20230809033000_20230809033417_4000M_VPRJ5.NC
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        String[] s = fileName.split("_");
        Map<String, Object> params = new HashMap<>();
        String satellite = s[0].replace("-", "");
        String level = s[5].replace("-", "");
        String region = s[3].replace("-", "");
        String product = s[6].replace("-", "");
        String date = s[9].substring(0, 8);
        String startTime = date + "000000";
        String endTime = date + "235959";
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findByMetadata(startTime, endTime, level, satellite,
                product, region, fileType);
        List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
        String join = StrUtil.join(",", collect);
        params.put("primaryFile", join);
        params.put("ShpFile", taskPathParam.getParameter03());
        params.put("SSLRID", taskPathParam.getParameter04());
        params.put("SpecialTopic", taskPathParam.getParameter05());
        params.put("Period", taskPathParam.getParameter06());
        params.put("RegionId", taskPathParam.getParameter07());
        params.put("Code", taskPathParam.getParameter08());
        params.put("CompositeType", taskPathParam.getParameter09());
        params.put("Scale", taskPathParam.getParameter10());
        params.put("DatasetNames", taskPathParam.getParameter11());
        params.put("ExportPng", Boolean.parseBoolean(taskPathParam.getParameter12()));
        params.put("ExportStat", Boolean.parseBoolean(taskPathParam.getParameter13()));
        algorithmParams.put("parameter00", adsType);
        algorithmParams.put("parameter01", params);
        return algorithmParams;
    }
}
